import $API from '@/api/services'
import { isWxBrowser } from '@/utils'
const shareurlSend = location.origin + location.pathname
let shareContent = {
  title: '',
  desc: '',
  link: '',
  imgUrl: '',
  success: function() {},
  cancel: function() {}
}

const wechat = {
  state: {
    cardData: null,
    cardList: [],
    cardCount: null,
    wxConfig: null,
    cardExt: null,
    panoWxCardList: null
  },
  actions: {
    // 获取作品中投放的卡券
    fetchPanoWxCard({ commit }, panoId) {
      panoId = panoId || this.getters.panoInfo.pano_id
      $API('getWxCards', [panoId]).then(res => {
        if (res.data) {
          commit('SAVE_PANO_WX_CARD', res.data)
        }
      })
    },
    fetchCardsInfo({ commit }) {
      let data = this.getters.panoWxCardList ? this.getters.panoWxCardList : []

      data.map(item => {
        $API('getCardInfo', [item.fk_card_id]).then(res => {
          if (res.data) {
            commit('SAVE_WECHAT_CARD', res.data)
          }
        })
      })
    },
    getCardInfo({ commit }, cardId) {
      $API('getAddCard', [cardId, decodeURIComponent($globalconfig.URLS.PANO_VIEW)]).then(res => {
        if (res.data) {

          const wxConfig = res.data.wxconfig
          wx.config({
            debug: false,
            appId: wxConfig.appid,
            timestamp: wxConfig.timestamp,
            nonceStr: wxConfig.noncestr,
            signature: wxConfig.signature,
            jsApiList: [
              'checkJsApi',
              'addCard',
              'chooseCard',
              'openCard'
            ]
          })

          this.dispatch('receiveWxCard', res.data.cardext)
          commit('SAVE_CARD_INFO', res.data)
        }
      })
    },
    receiveWxCard({ commit }, cardext) {
      let self = this
      wx.ready(() => {
        wx.addCard({
          cardList: [{
            cardId: cardext.card_id || '',
            cardExt: JSON.stringify({
              code: cardext.code || '',
              openid: cardext.openid || '',
              timestamp: cardext.timestamp,
              nonce_str: cardext.noncestr,
              fixed_begintimestamp: cardext.fixed_begintimestamp,
              signature: cardext.signature
            })
          }],
          success: function(res) {
            // console.log('微信卡券')
            var cardList = res.cardList; // 添加的卡券列表信息
            self._vm.$vgo.tip('领券成功，请到微信卡包中查看', 'success', 3000)
          }
        })
      })
    },
    // 修改微信分享的内容
    updateWxShareContents({ commit }, imgsrc) {
      if (imgsrc) {
        shareContent.imgUrl = imgsrc
      }
    },
    getWXShareData({ commit }, imgsrc = '') {
      if (!isWxBrowser()) return

      let panoInfo = this.getters.panoInfo
      let wx = window.wx
      $API('getOverviewWXShare', [shareurlSend]).then(res => {
        let data = res.data
        if(data) {
          wx.config({
            debug: false,
            // appId: data.appid,
            // timestamp: data.timestamp,
            // nonceStr: data.nonce,
            // signature: data.signature,
            appId: data.appId,
            timestamp: data.timestamp,
            nonceStr: data.nonceStr,
            signature: data.signature,
            jsApiList: [
              'onMenuShareTimeline',
              'openLocation',
              'getLocation',
              'onMenuShareAppMessage',
              'onMenuShareQQ',
              'onMenuShareQZone',
              'onMenuShareWeibo',
              'showOptionMenu'
            ]
          })

          shareContent.title = panoInfo.sharetitle || panoInfo.pano_name || ''
          shareContent.desc = panoInfo.sharecontent || ''
          shareContent.link = window.location.href
          shareContent.imgUrl = imgsrc || panoInfo.full_share_ico_url || panoInfo.full_icon_src || ''

          wx.ready(function() {
            wx.showOptionMenu()

            wx.onMenuShareTimeline(shareContent)

            //分享给朋友
            wx.onMenuShareAppMessage(shareContent)

            //QQ
            wx.onMenuShareQQ(shareContent)

            //QQ微博
            wx.onMenuShareWeibo(shareContent)

            //QQ空间
            wx.onMenuShareQZone(shareContent)

          })

          wx.error(function(res) {
            console.log(res)
          })
        }
      })
    }
  },
  mutations: {
    SAVE_WECHAT_CARD: (state, data) => {
      state.cardList.push(data)
    },
    SAVE_CARD_INFO: (state, data) => {
      state.wxConfig = data.wxconfig
      state.cardExt = data.cardext
    },
    SAVE_PANO_WX_CARD: (state, data) => {
      state.panoWxCardList = data
    }
  },
}

export default wechat
